#include "plotshowwidget.h"
#include "plotcurveviewitem.h"
#include <QListWidget>

PlotShowWidget::PlotShowWidget(QWidget *parent) :
    QWidget(parent)
{
	for (int i = 0; i < MaxPlotViewCount; i ++)
	{
		m_plotDataArr[i] = new PlotData();
		m_plotViewArr[i] = new PlotView(m_plotDataArr[i]);

		// add some fake curves
	    m_plotViewArr[i]->AddCurve("c1");
	    m_plotViewArr[i]->AddData(1, 2, 0);
	    m_plotViewArr[i]->AddData(2, 4, 0);

		m_curveShowArr[i] = new CurveShowWidget(m_plotDataArr[i]);
		m_plotViewArr[i]->setCurveShowWidget(m_curveShowArr[i]);
		QObject::connect(m_plotViewArr[i], SIGNAL(zoomStateChanged(bool)), this, SLOT(ZoomHandle(bool)));
		QObject::connect(m_curveShowArr[i], SIGNAL(checkStateChanged(std::string, bool)), m_plotViewArr[i], SLOT(SetCurveState(std::string, bool)));
	}

    this->setupUI();
}

void PlotShowWidget::setupUI()
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);

	gridLayout = new QGridLayout();
	gridLayout->addWidget(m_plotViewArr[0], 0, 0);
	gridLayout->addWidget(m_plotViewArr[1], 0, 1);
	gridLayout->addWidget(m_plotViewArr[2], 1, 0);
	gridLayout->addWidget(m_plotViewArr[3], 1, 1);

	mainLayout->addLayout(gridLayout);
    QFrame *line = new QFrame();
    line->setFrameShape(QFrame::HLine);
    line->setFrameShadow(QFrame::Sunken);
    mainLayout->addWidget(line);

	// add plot curve view
	hLayout = new QHBoxLayout();

	mainLayout->addLayout(hLayout);
}

// slots
void PlotShowWidget::ZoomHandle(bool zoom)
{
	PlotView *view = (PlotView *)(this->sender());
	if(zoom){ // zoom in
		view->setWidth(view->getWidth() * 2);
		view->setHeight(view->getHeight() * 3 / 2);
		for (int i = 0; i < MaxPlotViewCount; i ++)
		{
			gridLayout->removeWidget(m_plotViewArr[i]);
			m_plotViewArr[i]->hide();
		}
		
		view->show();
		gridLayout->addWidget(view, 0, 0, 2, 2);

		hLayout->addWidget(view->getCurveShowWidget());
		view->getCurveShowWidget()->show();
	} else { // zoom out
		view->setWidth(view->getWidth() / 2);
		view->setHeight(view->getHeight() * 2 / 3);
		gridLayout->removeWidget(view);
		view->hide();
		for (int i = 0; i < MaxPlotViewCount; i ++)
		{
			gridLayout->addWidget(m_plotViewArr[i]);
			m_plotViewArr[i]->show();
		}
		hLayout->removeWidget(view->getCurveShowWidget());
		view->getCurveShowWidget()->hide();
	}
}

// slots